import { ErrorBoundary } from "react-error-boundary";

export default function ErrorDemoPage() {
  return (
    <ErrorBoundary fallbackRender={({ error }) => <ErrorRenderer error={error} />}>
      <ThrowError throwError />
    </ErrorBoundary>
  );
}

function ThrowError({ throwError }: { throwError: boolean }) {
  if (throwError) {
    throw new Error("Aha!");
  }
  return <div>{throwError}</div>;
}

function ErrorRenderer({ error }: { error: unknown }) {
  if (error instanceof Error) {
    return <div>💥{error.message}</div>;
  }
  return <div>💥 unknown error!</div>;
}
